package com.leo;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @ClassName DraggedCopyDemo
 * @Description 自定义类型拖拽
 * @Author wangss
 * @date 2020.06.05 14:12
 * @Version 1.0
 */
public class DraggedCopyDemo3 extends Application {

    private DataFormat dataFormat = new DataFormat("data/person");

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Student student = new Student("wangss", "16", "file:D:\\图片保存\\小图片\\1.jpg");

        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        Button button = new Button(student.getName());

        VBox vBox = createVBox();

        AnchorPane.setTopAnchor(button, 100d);
        AnchorPane.setLeftAnchor(button, 20d);

        AnchorPane.setTopAnchor(vBox, 100d);
        AnchorPane.setLeftAnchor(vBox, 400d);

        an.getChildren().addAll(button, vBox);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setResizable(false);
        primaryStage.show();

        button.setOnDragDetected(event -> {
            Dragboard dragboard = button.startDragAndDrop(TransferMode.COPY_OR_MOVE);

            VBox view = createVBox(student);
            an.getChildren().add(view);
            WritableImage wi = new WritableImage((int) view.getPrefWidth(), (int) view.getPrefHeight());
            // 这里截图有一个问题，所截画面必须在面板上显示出来才可以截图
            view.snapshot(new SnapshotParameters(), wi);

            an.getChildren().remove(view);

            dragboard.setDragView(wi);

            ClipboardContent content = new ClipboardContent();
            content.put(dataFormat, student);

            dragboard.setContent(content);

        });

        vBox.setOnDragOver(event -> {
            event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
        });

        vBox.setOnDragDropped(event -> {
            Dragboard dragboard = event.getDragboard();

            Object content = dragboard.getContent(dataFormat);

            Student s = (Student) content;

            TextField t1 = (TextField) vBox.getChildren().get(1);
            TextField t2 = (TextField) vBox.getChildren().get(2);
            ImageView iv = (ImageView) vBox.getChildren().get(3);

            t1.setText(s.getName());
            t2.setText(s.getAge());
            iv.setImage(new Image(s.getPhoto()));

        });

    }

    private VBox createVBox() {
        VBox vBox = createVBox(null);

        vBox.setStyle("-fx-background-color: #fff2f2; -fx-border-color: #FF845D;");

        return vBox;
    }

    private VBox createVBox(Student student) {
        VBox vBox = new VBox(20d);

        vBox.setPrefWidth(300d);
        vBox.setPrefHeight(500d);

        Button button = new Button("个人详细信息");
        button.prefWidthProperty().bind(vBox.prefWidthProperty());

        TextField t1 = new TextField();
        t1.setAlignment(Pos.CENTER);
        t1.setDisable(true);

        TextField t2 = new TextField();
        t2.setAlignment(Pos.CENTER);

        ImageView iv = new ImageView();
        iv.setPreserveRatio(true);
        iv.setFitWidth(vBox.getPrefWidth());

        vBox.getChildren().addAll(button, t1, t2, iv);

        if (student != null) {
            t1.setText(student.getName());
            t2.setText(student.getAge());
            iv.setImage(new Image(student.getPhoto()));
        }

        return vBox;
    }
}
